﻿using Newtonsoft.Json;
using XYS.Core.Util;

namespace XYS.Service.Core.Util.Converters;

/// <summary>
/// long 类型转 string 转换器
/// </summary>
public class LongToStringConverter : JsonConverter<long>
{
    /// <summary>
    /// 读
    /// </summary>
    /// <param name="reader"></param>
    /// <param name="objectType"></param>
    /// <param name="existingValue"></param>
    /// <param name="hasExistingValue"></param>
    /// <param name="serializer"></param>
    /// <returns></returns>
    /// <exception cref="JsonSerializationException"></exception>
    public override long ReadJson(JsonReader reader, Type objectType, long existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        // Implement the deserialization logic here
        if (reader.TokenType == JsonToken.Integer)
        {
            return (long)reader.Value;
        }

        if (reader.TokenType == JsonToken.String)
        {
            string valueString = reader.Value.ToString();
            if (valueString.IsNullOrWhiteSpace()) return 0;

            if (long.TryParse(valueString, out long result))
                return result;
        }

        throw new JsonSerializationException($"类型转换器 转换失败,目标类型[long]. Token: {reader.TokenType}, Value: {reader.Value}");
    }

    /// <summary>
    /// 写
    /// </summary>
    /// <param name="writer"></param>
    /// <param name="value"></param>
    /// <param name="serializer"></param>
    public override void WriteJson(JsonWriter writer, long value, JsonSerializer serializer)
    {
        writer.WriteValue(value.ToString());
    }
}


/// <summary>
/// long 类型转 string 转换器
/// </summary>
public class LongCanNullToStringConverter : JsonConverter<long?>
{
    /// <summary>
    /// 读
    /// </summary>
    /// <param name="reader"></param>
    /// <param name="objectType"></param>
    /// <param name="existingValue"></param>
    /// <param name="hasExistingValue"></param>
    /// <param name="serializer"></param>
    /// <returns></returns>
    /// <exception cref="JsonSerializationException"></exception>
    public override long? ReadJson(JsonReader reader, Type objectType, long? existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        if (reader.Value == null) return null;

        if (reader.TokenType == JsonToken.Integer) { return (long)reader.Value; }

        if (reader.TokenType == JsonToken.String)
        {
            string readerValue = (string)reader.Value;
            if (readerValue.IsNullOrWhiteSpace())
                return null;

            if (long.TryParse(readerValue, out long result))
            {
                return result;
            }
        }

        throw new JsonSerializationException($"类型转换器 转换失败,目标类型[long?]. Token: {reader.TokenType}, Value: {reader.Value}");
    }

    /// <summary>
    /// 写
    /// </summary>
    /// <param name="writer"></param>
    /// <param name="value"></param>
    /// <param name="serializer"></param>
    public override void WriteJson(JsonWriter writer, long? value, JsonSerializer serializer)
    {
        if (value != null)
            writer.WriteValue(value.ToString());
    }
}
